﻿@model ConfigurationModel

@{
    Layout = "_ConfigureModule";

    var gridViewData = new ViewDataDictionary(this.ViewData);
    gridViewData["Parent"] = Model;
}

<widget target-zone="admin_button_toolbar_before">
    <button id="SaveConfigButton" type="submit" name="save" class="btn btn-warning" value="save">
        <i class="fa fa-check"></i>
        <span>@T("Admin.Common.Save")</span>
    </button>
</widget>

<tabstrip id="webapi-config" sm-nav-style="Material" sm-nav-position="Top">
    <tab sm-title="@T("Common.General")" sm-selected="true">
        @await Component.InvokeAsync("StoreScope")
        <form asp-action="Configure">
            <div asp-validation-summary="All"></div>

            <div class="adminContent">
                <div class="adminRow">
                    <div class="adminTitle">
                        <smart-label asp-for="ApiOdataUrl" />
                    </div>
                    <div class="adminData form-control-plaintext">
                        <a href="@Model.ApiOdataUrl" target="_blank">@Model.ApiOdataUrl</a>
                    </div>
                </div>
                <div class="adminRow">
                    <div class="adminTitle">
                        <smart-label asp-for="ApiOdataMetadataUrl" />
                    </div>
                    <div class="adminData form-control-plaintext">
                        <a href="@Model.ApiOdataMetadataUrl" target="_blank">@Model.ApiOdataMetadataUrl</a>
                    </div>
                </div>
                <div sm-if="Model.ApiOdataEndpointsUrl.HasValue()" class="adminRow">
                    <div class="adminTitle">
                        <smart-label asp-for="ApiOdataEndpointsUrl" />
                    </div>
                    <div class="adminData form-control-plaintext">
                        <a href="@Model.ApiOdataEndpointsUrl" target="_blank">@Model.ApiOdataEndpointsUrl</a>
                    </div>
                </div>
                <div class="adminRow">
                    <div class="adminTitle">
                        <smart-label asp-for="ApiDocsUrl" />
                    </div>
                    <div class="adminData form-control-plaintext">
                        <a href="@Model.ApiDocsUrl" target="_blank">@Model.ApiDocsUrl</a>
                    </div>
                </div>

                <div class="adminRow">
                    <div class="adminTitle">
                        <smart-label asp-for="IsActive" />
                    </div>
                    <div class="adminData">
                        <setting-editor asp-for="IsActive" data-toggler-for=".api-settings"></setting-editor>
                        <span asp-validation-for="IsActive"></span>
                    </div>
                </div>
                <div class="api-settings">
                    <div class="adminRow">
                        <div class="adminTitle">
                            <smart-label asp-for="MaxTop" />
                        </div>
                        <div class="adminData">
                            <setting-editor asp-for="MaxTop"></setting-editor>
                            <span asp-validation-for="MaxTop"></span>
                        </div>
                    </div>
                    <div class="adminRow">
                        <div class="adminTitle">
                            <smart-label asp-for="MaxExpansionDepth" />
                        </div>
                        <div class="adminData">
                            <setting-editor asp-for="MaxExpansionDepth"></setting-editor>
                            <span asp-validation-for="MaxExpansionDepth"></span>
                        </div>
                    </div>
                    <div class="adminRow">
                        <div class="admin-config-group">
                            <div class="title">@T("Plugins.Api.WebApi.Batch")</div>
                        </div>
                    </div>

                    <div class="adminRow">
                        <div class="adminTitle">
                            <smart-label asp-for="MaxBatchNestingDepth" />
                        </div>
                        <div class="adminData form-control-plaintext">
                            <editor asp-for="MaxBatchNestingDepth" />
                            <span asp-validation-for="MaxBatchNestingDepth"></span>
                        </div>
                    </div>
                    <div class="adminRow">
                        <div class="adminTitle">
                            <smart-label asp-for="MaxBatchOperationsPerChangeset" />
                        </div>
                        <div class="adminData form-control-plaintext">
                            <editor asp-for="MaxBatchOperationsPerChangeset" />
                            <span asp-validation-for="MaxBatchOperationsPerChangeset"></span>
                        </div>
                    </div>
                    <div class="adminRow">
                        <div class="adminTitle">
                            <smart-label asp-for="MaxBatchReceivedMessageSize" />
                        </div>
                        <div class="adminData form-control-plaintext">
                            <editor asp-for="MaxBatchReceivedMessageSize" sm-postfix="KB" />
                            <span asp-validation-for="MaxBatchReceivedMessageSize"></span>
                        </div>
                    </div>
                </div>

            </div>
        </form>
    </tab>

    <tab sm-title="@T("Plugins.Api.WebApi.UserGridTabTitle")" sm-adaptive-height="true">
        <div class="d-flex flex-column h-100">
            <div class="flex-grow-0">
                <div class="alert alert-info alert-dismissible">
                    <button type="button" class="btn-close" data-dismiss="alert"></button>
                    @Html.Raw(T("Plugins.Api.WebApi.AdminInstruction"))
                </div>
            </div>
            <div class="flex-grow-1 position-relative" style="overflow: hidden;">
                <partial name="_Grid.Users" model="null" view-data="gridViewData" />
            </div>
        </div>
    </tab>
</tabstrip>

<partial name="_ApiKeysPopup" model="new ApiKeysModel()" />

<script sm-target-zone="scripts" data-origin="webapi-config">
    $(function () {

        $(document).on('click', '.apiuser-grid-cmd', function (e) {
            e.preventDefault();
            let btn = $(this);
            let ask = @T("Admin.Common.AskToProceed").JsValue;
            let createKeys = btn.hasClass('create-keys');
            let showKeys = btn.hasClass('show-keys');
            let customerId = btn.data('key');

            $({}).ajax({
                cache: false,
                type: 'POST',
                url: btn.data('url'),
                data: { "customerId": customerId },
                ask: createKeys || btn.hasClass('delete-keys') ? ask : null,
                success: function (response) {
                    if (createKeys || showKeys) {
                        var dialog = $('#webapi-keys-dialog');
                        dialog.find('.modal-body').first().html(response);

                        applyCommonPlugins(dialog);
                        dialog.modal('show');
                    }

                    @*if (createKeys) {
                        var gridRow = $('#apiuser-grid').find('tr[data-key=' + customerId + ']').first();
                        var userInfo = gridRow.find('.apiuser-info');
                        var elPublicKey = $('#webapi-copy-publickey');
                        var enabled = elPublicKey.closest('.adminContent').data('enabled');
                        var yes = T("Admin.Common.Yes").JsValue;
                        var no = T("Admin.Common.No").JsValue;

                        userInfo.find('.apiuser-publickey').text(elPublicKey.data('apikey'));
                        userInfo.find('.apiuser-secretkey').text($('#webapi-copy-secretkey').data('apikey'));
                        userInfo.find('.apiuser-enabled').text(enabled ? yes : no);
                        userInfo.find('.apiuser-lastrequest').text('-');

                        gridRow.removeClass('text-muted');
                        userInfo.show();
                    } else*@
                    if (!showKeys) {
                        const grid = $('#apiuser-grid').parent().data('datagrid');
                        grid.read();
                    }
                }
            });
        });
    });
</script>